/* eslint-disable no-undef */
import router from './router'
import store from './store'


const isTest = process.env.NODE_ENV === 'production'

let userID

router.beforeEach((to, from, next) => {
  let anfunc = function () {
    store.dispatch('GetInfo').then(res => { // 先获取用户信息
      const avatar = res.result.roleID // 角色ID
      store.dispatch('GenerateRoutes', avatar).then(() => { // 生成可访问的路由表
        router.addRoutes(store.getters.addRouters) // 动态添加可访问路由表

        if (avatar == 5 || avatar == 6) { // 自动跳转
          next({ path: '/task' })
        }

        next({ ...to, replace: true }) // hack方法 确保addRoutes已完成 ,set the replace: true so the navigation will not leave a history record
      })

      next()
    })
  }

  if (store.state.user.userID == '') {
    if (isTest) {
      vrv.init({
        debug: false
      })
      vrv.ready(function () {
        vrv.jssdk.getAccountInfo({
          success: function (r) {
            userID = r.mUserId
            store.commit('SET_USERID', userID)
            anfunc()
          }
        })
      })
    } else {// 测试  开发环境
      userID = 20904511139// 1领导(20904511138) 2值班员(20451526342/20904511139) 3地方网信办(20451526349/20904511144) 4运营单位(20904511153) 5安全厂商(20904511152/20904511152) 6专家(20904511200,20904511141)
      store.commit('SET_USERID', userID)
      anfunc()
    }
  } else {
    next()
  }
})
router.afterEach(() => {

})

